<template>
  <div class="view-account">
    <img class="view-account-loginBG" src="~@/assets/images/login_bg1.jpg" alt="" />
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo">
          <!-- <img :src="websiteConfig.loginImage" alt="" /> -->
        </div>
        <div class="view-account-top-desc">{{ websiteConfig.loginDesc }}</div>
      </div>
      <div class="view-account-form">
        <n-form
          ref="formRef"
          label-placement="left"
          size="large"
          :model="formInline"
          :rules="rules"
        >
          <n-form-item path="phone">
            <n-input v-model:value="formInline.phone" placeholder="请输入手机号">
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <PersonOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item path="password">
            <n-input
              v-model:value="formInline.password"
              type="password"
              showPasswordOn="click"
              @keyup.enter="handleSubmit"
              placeholder="请输入密码"
            >
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item class="default-color">
            <div class="flex justify-between">
              <div class="flex-initial">
                <n-checkbox v-model:checked="savePassword">记住密码</n-checkbox>
              </div>
            </div>
          </n-form-item>
          <n-form-item>
            <n-button type="warning" @click="handleSubmit" size="large" :loading="loading" block>
              登录
            </n-button>
          </n-form-item>
          <!--<n-form-item class="default-color">-->
            <!--<div class="flex view-account-other">-->
              <!--<div class="flex-initial">-->
                <!--<span>其它登录方式</span>-->
              <!--</div>-->
              <!--<div class="flex-initial mx-2">-->
                <!--<a href="javascript:">-->
                  <!--<n-icon size="24" color="#2d8cf0">-->
                    <!--<LogoGithub />-->
                  <!--</n-icon>-->
                <!--</a>-->
              <!--</div>-->
              <!--<div class="flex-initial mx-2">-->
                <!--<a href="javascript:">-->
                  <!--<n-icon size="24" color="#2d8cf0">-->
                    <!--<LogoFacebook />-->
                  <!--</n-icon>-->
                <!--</a>-->
              <!--</div>-->
              <!--<div class="flex-initial" style="margin-left: auto">-->
                <!--<a href="javascript:">注册账号</a>-->
              <!--</div>-->
            <!--</div>-->
          <!--</n-form-item>-->
        </n-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive,watch, ref, onMounted } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import { useUserStore } from '@/store/modules/user';
  import { useMessage } from 'naive-ui';
  import { ResultEnum } from '@/enums/httpEnum';
  import { PersonOutline, LockClosedOutline } from '@vicons/ionicons5';
  import { PageEnum } from '@/enums/pageEnum';
  import { websiteConfig } from '@/config/website.config';
  interface FormState {
    phone: string;
    password: string;
  }

  const formRef = ref();
  const message = useMessage();
  const loading = ref(false);
  const savePassword = ref(false);
  onMounted(() => {
    getPassWord();
  })
  //检测是否选中
  watch(
        () => savePassword.value,
        (isRemember) => {
          if (!isRemember) {
            localStorage.removeItem("rememberPassWord");
          }
        //  console.log("isRemember=",isRemember);
        }
      );
  // 获取记住的密码
  const getPassWord = ()=> {
    let pw = localStorage.getItem("rememberPassWord");
      if (pw) {
        const { userId, password } = JSON.parse(pw);
        formInline.phone = userId;
        formInline.password= password;
        savePassword.value = true;
      }
      else
      {
        formInline.phone = "";
        formInline.password = "";
      }
  }
  const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;
  const enum  LoginEnum{
      ENABLE_LOGIN="able",
      DISABLE_LOGIN="disable",
  }

  const formInline = reactive({
    password: '',
    phone: '',
    isCaptcha: true,
  });

  const rules = {
    phone: { required: true, message: '请输入电话号码', trigger: 'blur' },
    password: { required: true, message: '请输入密码', trigger: 'blur' },
  };

  const userStore = useUserStore();

  const router = useRouter();
  const route = useRoute();

  const handleSubmit = (e) => {
    e.preventDefault();
    formRef.value.validate(async (errors) => {
      if (!errors) {
        const { phone, password } = formInline;
        message.loading('登录中...');
        loading.value = true;
        const params: FormState = {
          phone,
          password,
        };

        try {
          const { code, message: msg,meta } = await userStore.login(params);
          message.destroyAll();
          if (code == ResultEnum.SUCCESS) {
            if(meta.able==LoginEnum.DISABLE_LOGIN){
              message.info(msg || '用户被禁用，不可用');
              return
            }
            if (savePassword.value) {
                  localStorage.setItem(
                    "rememberPassWord",
                    JSON.stringify({
                      userId: formInline.phone,
                      password: formInline.password,
                    })
                  );
                }
            const toPath = decodeURIComponent((route.query?.redirect || '/') as string);
            message.success('登录成功，即将进入系统');
            if (route.name === LOGIN_NAME) {
              router.replace('/');
            } else router.replace(toPath);
          } else {
            message.info(msg || '登录失败');
          }
        } finally {
          loading.value = false;
        }
      } else {
        message.error('请填写完整信息，并且进行验证码校验');
      }
    });
  };
</script>

<style lang="less" scoped>
  .view-account {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: auto;
    align-items: center;
    justify-content: center;
    &-loginBG {
      width: 100%;
      height: 100%;
      position: fixed;
      top: 0;
      left: 0;
      z-index: -1;
    }
    &-container {
      // flex: 1;
      padding: 12px 32px;
      max-width: 424px;
      min-width: 360px;
      height: 420px;
      margin: 0 auto;
      border-radius: 16px;
      background: #fff;
    }

    &-top {
      padding: 32px 0;
      text-align: center;

      &-desc {
        font-size: 20px;
        color: #000000;
      }
    }

    &-other {
      width: 100%;
    }

    .default-color {
      color: #515a6e;

      .ant-checkbox-wrapper {
        color: #515a6e;
      }
    }
  }

  @media (min-width: 768px) {
    .view-account {
      background-image: url('../../assets/images/login.svg');
      background-repeat: no-repeat;
      background-position: 50%;
      background-size: 100%;
    }

    .page-account-container {
      padding: 32px 0 24px 0;
    }
  }
</style>
